package prog.model;


import prog.view.ProjectPropertyForm;
import prog.view.MainForm;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author MrKhoai
 */
public class Program {
    public ProjectPropertyForm projectPropertyForm;
    public MainForm mainForm;
    private ArrayList<Project> projects;

    public Program() {}

    public void initInstance() {
        projects = new ArrayList<Project>();
        mainForm = new MainForm();
        projectPropertyForm = new ProjectPropertyForm();
    }

    public void runProject(final int index) {
        if(index >= projects.size() || index < 0) {
            return;
        }
        Thread thread = new Thread(new Runnable() {

            public void run() {
                try {
                    Project pj = projects.get(index);
                    pj.setStatus(Project.RUN);
                    pj.work();
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    public void startAllProject() {
        for(int i = 0; i < projects.size(); i++) {
            if(projects.get(i).getStatus() != Project.RUN)
                runProject(i);
        }
    }

    public void stopAllProject() {
        for(int i = 0; i < projects.size(); i++) {
            if(projects.get(i).getStatus() == Project.RUN)
                stopProject(i);
        }
    }

    public void stopProject(int index) {
        if(index < projects.size() || index < 0) {
            projects.get(index).setStatus(Project.STOP);
            saveInstance();
        }
    }

    public void pauseProject(int index) {
        if(index < projects.size() || index < 0) {
            projects.get(index).setStatus(Project.PAUSE);
            saveInstance();
        }
    }

    public void resumeProject(int index) {
        runProject(index);
    }

    public void newProject(Project pj) {
        projects.add(pj);
        pj.setStatus(Project.NONE);
        saveInstance();
    }

    public void removeProject(int index) {
        if(index < projects.size() || index < 0) {
            projects.remove(index);
            saveInstance();
        }
    }

    public Project getProject(int index) {
        if(index >= projects.size() || index < 0)
            return null;
        return projects.get(index);
    }

    public int getProjectCount() {
        return projects.size();
    }

    public void saveInstance() {
        try {
            File f = new File("data");
            if(!f.exists())
                f.mkdir();
            BufferedWriter writer = new BufferedWriter(new FileWriter("data/Data.a"));
            for(Project pj : projects) {
                String name = pj.getName();
                String url = pj.getURL();
                String partern = pj.getPartern();
                String dst = pj.getDst();
                Integer lvLimit = pj.getLv();
                Integer inOps = pj.getInsOp();
                Integer downOp = pj.getdownOp();
                Integer status = pj.getStatus();
                if(status == 1)
                    status = 3;
                writer.write(name);
                writer.newLine();
                writer.write(url);
                writer.newLine();
                writer.write(dst);
                writer.newLine();
                writer.write(lvLimit.toString());
                writer.newLine();
                writer.write(inOps.toString());
                writer.newLine();
                writer.write(downOp.toString());
                writer.newLine();
                writer.write(status.toString());
                writer.newLine();
                writer.write(partern);
                writer.newLine();
                writer.flush();
            }
            writer.close();
        } catch (Exception e) {

        }
    }

    public void loadInstance() {
        try {
            File f = new File("data/Data.a");
            BufferedReader reader = null;
            if(f.exists()) {
                reader = new BufferedReader(new FileReader("data/Data.a"));
                String line = reader.readLine();
                while(line != null) {
                    Project pj = new Project();
                    pj.setName(line);
                    pj.setURL(reader.readLine());
                    pj.setDst(reader.readLine());
                    pj.setLv(Integer.parseInt(reader.readLine()));
                    pj.setInsOp(Integer.parseInt(reader.readLine()));
                    pj.setdownOp(Integer.parseInt(reader.readLine()));
                    pj.setStatus(Integer.parseInt(reader.readLine()));
                    pj.setPartern(reader.readLine());
                    projects.add(pj);
                    line = reader.readLine();
                }
            }

            for(Project pj:projects) {
                // Can xu ly cho nay.
            }
        }
        catch(Exception e) {

        }
    }
}
